/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.sunspotworld.demo;

/**
 *
 * @author me
 */
public class CowRecord {
    public static final int RECORD_SIZE = 2*4 + 2*8;
    
    private int count;
    private int sample;
    private double avgRSSI;
    private double avgCCI;
    
    public CowRecord(int c, int s, double r, double cc) {
        count = c;
        sample = s;
        avgRSSI = r;
        avgCCI = cc;
    }
    
    public CowRecord(byte[] record) {
        int index = 0;
        count = byteArrayToInt(record, index);
        index += 4;
        avgRSSI = byteArrayToDouble(record, index);
        index += 8;
        avgCCI = byteArrayToDouble(record, index);
    }
    
    private int byteArrayToInt(byte [] b, int startIndex) {
        return (b[startIndex + 0] << 24)
                + ((b[startIndex + 1] & 0xFF) << 16)
                + ((b[startIndex + 2] & 0xFF) << 8)
                + (b[startIndex + 3] & 0xFF);
    }
    
    private static long byteArrayToLong(byte[] b, int startIndex) {
        return ((long)b[startIndex + 0] << 56)
                + (((long)b[startIndex + 1] & 0xFF) << 48)
                + (((long)b[startIndex + 2] & 0xFF) << 40)
                + (((long)b[startIndex + 3] & 0xFF) << 32)
                + (((long)b[startIndex + 4] & 0xFF) << 24)
                + (((long)b[startIndex + 5] & 0xFF) << 16)
                + (((long)b[startIndex + 6] & 0xFF) << 8)
                + ((long)b[startIndex + 7] & 0xFF);
    }
    
    private double byteArrayToDouble(byte[] b, int startIndex) {
        return Double.longBitsToDouble(byteArrayToLong(b, startIndex));
    }
    
    public byte[] toByteArray() {
        byte[] out = new byte[RECORD_SIZE];
        byte[] temp = intToByteArray(count);
        int index = 0;
        for (int i=0; i<4; i++) {
            out[index+i] = temp[i];
        }
        index += 4;
        temp = doubleToByteArray(avgRSSI);
        for(int i=0; i<8; i++) {
            out[index+i]=temp[i];
        }
        index += 8;
        temp = doubleToByteArray(avgCCI);
        for(int i=0; i<8; i++) {
            out[index+i]=temp[i];
        }
        return out;
    }
    
    private byte[] intToByteArray(int value) {
        return new byte[] {
                (byte)(value >>> 24),
                (byte)(value >>> 16),
                (byte)(value >>> 8),
                (byte)value};
    }
    
    private byte[] doubleToByteArray(double value) {
        return longToByteArray(Double.doubleToLongBits(value));
    }
    
    private byte[] longToByteArray(long value) {
        return new byte[] {
                (byte)(value >>> 56),
                (byte)(value >>> 48),
                (byte)(value >>> 40),
                (byte)(value >>> 32),
                (byte)(value >>> 24),
                (byte)(value >>> 16),
                (byte)(value >>> 8),
                (byte)value};
    }
    
    public static String getHeader() {
        return "Count\tOut Of\tAvg RSSI\tAvgCCI";
    }
    
    public String toString() {
        return count + "\t" + sample + "\t" + avgRSSI + "\t" + avgCCI;
    }

}
